package com.itboye.mengwu.app.login.ui;

import java.util.Observable;
import java.util.Observer;

import android.app.AlertDialog;
import android.app.usage.UsageEvents.Event;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.model.PersonDataBean;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;
import com.itboye.mengwu.data.repository.YanzhengmaRespository;

public class BindTelephoneActivity extends BaseActivity implements
		OnClickListener, Observer {
	EditText bindmobil_input_phonenum, bindmobil_input_password,
			bindmobil_test_yzm;
	TextView getYzm;
	Button bindmobil_bind_now;
	private MainPresenter mainPresenter;
	private GetYanzhengMaPresenter getYanzhengMaPresenter;
	private String code;
	private String phoneNumber;
	private String password;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.bind_mobil;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		mainPresenter = new MainPresenter(this);
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.getYzm:
			phoneNumber = bindmobil_input_phonenum.getText().toString().trim();
			if (phoneNumber == null) {
				ByAlert.alert("请输入号码");
			} else {
				getYanzhengMaPresenter.getVerificationCode(phoneNumber, 3 + "");
			}
			break;
		case R.id.bindmobil_bind_now:
			phoneNumber = bindmobil_input_phonenum.getText().toString().trim();
			if (phoneNumber == null) {
				ByAlert.alert("请输入号码");
				return;
			}
			password = bindmobil_input_password.getText().toString().trim();
			if (password == null) {
				ByAlert.alert("请输入密码");
				return;
			}
			code = bindmobil_test_yzm.getText().toString().trim();
			if (code == null) {
				ByAlert.alert("请输入验证码");
				return;
			}
			PersonDataBean model = new PersonDataBean();
			model.setType(4);
			model.setUid((String) SPUtils.get(BindTelephoneActivity.this, null,
					SpPublicParams.UID, ""));
			model.setPassword(password);
			model.setYanzhengma(code);
			model.setMobile(phoneNumber);
			mainPresenter.bindPhoneNumber(model);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ByAlert.alert("必须绑定手机号哦");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(arg1);
		if (entity != null) {
			if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
				code = entity.getData().toString();
				bindmobil_test_yzm.setText(code);
			}
			if (entity.getEventType() == MainPresenter.BindPhoneNumberSuccessEvent) {
				ByAlert.alert(entity.getData().toString() + "WX");
				finish();
			}
		}
	}

}
